abstract class Chess {
  x: number = 0;
  y: number = 0;

  abstract readonly name: string;

  // 模板模式
  move(targetX: number, targetY: number): boolean {
    console.log("1. 边界判断");
    console.log("2. 目标位子判断");
    // 3. 规则判断
    if (this.rule(targetX, targetY)) {
      this.x = targetX;
      this.y = targetY;
      return true;
    }
    return false;
  }

  protected abstract rule(targetX: number, targetY: number): boolean;
}

class Horse extends Chess {
  readonly name: string = "马";

  move(targetX: number, targetY: number): boolean {
    this.x = targetX;
    this.y = targetY;
    console.log("马移动成功", targetX, targetY);
    return true;
  }

  protected rule(targetX: number, targetY: number): boolean {
    return true;
  }
}

class Pao extends Chess {
  readonly name: string;

  constructor() {
    super();
    this.name = "炮";
  }

  move(targetX: number, targetY: number): boolean {
    this.x = targetX;
    this.y = targetY;
    console.log("炮移动成功", targetX, targetY);
    return true;
  }

  protected rule(targetX: number, targetY: number): boolean {
    return true;
  }
}

class Soldier extends Chess {
  get name() {
    return "兵";
  }

  move(targetX: number, targetY: number): boolean {
    this.x = targetX;
    this.y = targetY;
    console.log("兵移动成功", targetX, targetY);
    return true;
  }

  protected rule(targetX: number, targetY: number): boolean {
    return true;
  }
}

export {};
